<template>
  <div>
    <el-form
      size="mini"
      :inline="true"
      ref="category"
      :model="category"
      style="margin: 20px 0"
      class="demo-form-inline"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="category.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="category.status" placeholder="请选择">
          <el-option label="禁用" value="0"></el-option>
          <el-option label="正常" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="resetForm('category')">重置</el-button>
        <el-button type="primary" @click="handleOpen">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table :data="categoryList" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="55"> </el-table-column>
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 0">禁用</el-tag>
          <el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delCategory(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="category.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="category.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form ref="categoryForm" :model="categoryForm" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="categoryForm.name"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="categoryForm.status" :label="1">正常</el-radio>
          <el-radio v-model="categoryForm.status" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="categoryForm.sort"
            :min="0"
            :max="100"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="categoryForm.remark"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-if="title == '新增'" @click="addCategory">确 定</el-button>
        <el-button type="primary" v-if="title == '编辑'" @click="editCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryList,
  delCategory,
  addCategory,
  searchCategory,
  editCategory,
} from "../../api/blog";
export default {
  data() {
    return {
      dialogVisible: false,
      categoryList: [],
      category: {
        current: 1,
        size: 20,
        name: "",
        status: "",
      },
      total: 0,
      categoryForm: {
        id: "",
        name: "",
        status: "",
        sort: 0,
        remark: "",
      },
      title: "",
    };
  },
  mounted() {
    this.getCategoryList();
  },
  methods: {
    async getCategoryList() {
      let res = await getCategoryList(this.category);
      console.log(res);
      this.categoryList = res.data.data.records;
      this.total = res.data.data.total;
    },
    search() {
      this.getCategoryList();
      this.$refs["category"].resetFields();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSizeChange(val) {
      this.category.size = val;
      this.getCategoryList();
    },
    handleCurrentChange(val) {
      this.category.page = val;
      this.getCategoryList();
    },
    async delCategory(id) {
      let res = await delCategory(id);
      console.log(res);
      this.getCategoryList();
    },
    handleOpen() {
      this.dialogVisible = true;
      this.title = "新增";
    },
    async addCategory() {
      let res = await addCategory(this.categoryForm);
      console.log(res);
      this.dialogVisible = false;
      this.getCategoryList();
    },
    async handleEdit(id){
        this.dialogVisible = true
        this.title = '编辑'
        let res = await searchCategory(id);
        this.categoryForm = res.data.data
    },
    async editCategory(){
        let res = await editCategory(this.categoryForm);
        // console.log(res);
        this.getCategoryList();
        this.dialogVisible = false;
    }
  },
};
</script>

<style scoped >
</style>
